package com.chameleon.sqltranslator;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.chameleon.sqltranslator.dialect.mysql.MySqlToOpenGaussOutputVisitor;

import java.util.List;

public class ExecuteTranslate {

    public static void main(String[] args) {
        System.out.println(Translate_MySQL2openGuass(args[0]));
    }

    public static String Translate_MySQL2openGuass(String Sql_in) {
        final StringBuilder appender = new StringBuilder();
        final MySqlToOpenGaussOutputVisitor visitor = new MySqlToOpenGaussOutputVisitor(appender);

        final List<SQLStatement> sqlStatements = SQLUtils.parseStatements(Sql_in, DbType.mysql);

        final StringBuilder res = new StringBuilder();

        for (SQLStatement statement : sqlStatements) {
            statement.accept(visitor);
            visitor.println();
            res.append(appender.toString());
            appender.delete(0,appender.length());
        }

        return res.toString();
    }

    public static void Translate_MySQL2openGuassDebug(String Sql_in) {

        final StringBuilder appender = new StringBuilder();
        final MySqlToOpenGaussOutputVisitor visitor = new MySqlToOpenGaussOutputVisitor(appender);

        final List<SQLStatement> sqlStatements = SQLUtils.parseStatements(Sql_in, DbType.mysql);

        for (SQLStatement statement : sqlStatements) {
            statement.accept(visitor);
            visitor.println();
            System.out.println(appender.toString());
            appender.delete(0,appender.length());
        }
    }

}
